package com.ioidea.ble.proto

import com.ioidea.base.util.MyLogger
import com.ioidea.base.util.Symbol

/**
 * 数据：SN     组ID    读/写
    12byte  12byte  2byte

    读/写：01：设置 00：读取

    示例：手机->设备
    SET_DEVICE_GROUP:A172508551F200000000000301
    示例：设备->手机
    STA_GROUP: A172508551F2 000000000001

    实例：读取
    示例：手机->设备
    SET_DEVICE_GROUP:A172508551F200000000000100
    示例：设备->手机
    STA_GROUP: A172508551F2 000000000001
 */
class ProtoDevMoveGroup(override var requestCommand: String="STA_GROUP",
                        override var responseCommand: String="STA_GROUP") : ProtoBase() {

  companion object{
      //读
      const val READ="0"
      //设置
      const val SET="1"
  }


    fun parseGroupDeviceResponse( wholeData: String) :GroupChildDevice? {
        //去头尾
        val data = removeHeadTail(wholeData)
        MyLogger.hLog().e("data:$data")
        //后面可以校验数据长度是否正确 暂时让数据有问题的奔溃 发现问题
        val dataArray= data.split(Symbol.SPACE)
        if (dataArray.size==2){
            val childDevSn=dataArray[0]
            val groupId=dataArray[1].toInt()
            return GroupChildDevice(childDevSn,groupId)
        }
        return null

    }

    /**
     * @param lightSn 灯的sn
     * @param groupId 组id
     * @param readSetType  01 设置  00读取
     */
    fun  getPayload(lightSn:String,groupId :Int,readSetType:String):String{
//        val groupId12 = "%012d".format(groupId)
        val payLoadBuilder=StringBuilder()
        payLoadBuilder.append(lightSn)
            .append(Symbol.SPACE)
            .append(groupId)
            .append(Symbol.SPACE)
            .append(readSetType)
        return payLoadBuilder.toString()
    }
}




